package com.kaixinff.kaixin001.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaixinff.kaixin001.util.Filter;
import com.kaixinff.kaixin001.util.KXClient;
import com.kaixinff.kaixin001.util.KXUtil;
import com.kaixinff.kaixin001.util.RegStep;
import com.kaixinff.name.User;
import com.kaixinff.net.HttpResponse;
import com.kaixinff.net.NameValuePair;

@SuppressWarnings("serial")
public class AddXWServlet extends HttpServlet {
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("text/html; charset=" + KXUtil.CHARSET);
		List<User> users = KXUtil.getUsers(new Filter(){
			public boolean access(User user) {
				return true;
			}
		}, resp);
		if (users == null) {
			return;
		}
		for (User user: users) {
			if (user.hasStep(RegStep.ADD_FRIEND_CONFIRM) && !user.hasStep(RegStep.ADD_XW)) {
				addXW(users, user);
			}
		}
	}
	
	private void addXW(List<User> users, User user) throws IOException {
		KXClient client = KXClient.getInstance(user.getId());
		String url = KXUtil.HOST_URL + "/!spiderman/invite.php?from=team&inviteuid=" + KXUtil.getMainUserUid() + "&fromuid=" + KXUtil.getMainUserUid();
		String html = client.doGet(url, null).getContent();
		Pattern p = Pattern.compile("window[.]location = '/app/[?]aid=(.+?)&from=(.+?)';");
		Matcher m = p.matcher(html);
		boolean isChanged = false;
		try {
			if (m.find()) {
				String aid = m.group(1);
				url = KXUtil.HOST_URL + "/app/?aid=" + aid + "&from=" + KXUtil.getMainUserUid();
				client.doGet(url, null);
				String referer = url;
				url = KXUtil.HOST_URL + "/app/install.php";
				List<NameValuePair> params = new ArrayList<NameValuePair>();
				params.add(new NameValuePair("aid", aid));
				params.add(new NameValuePair("aid2", "0"));
				params.add(new NameValuePair("from", KXUtil.getMainUserUid()));
				params.add(new NameValuePair("invitekey", ""));
				params.add(new NameValuePair("isinstall", "1"));
				params.add(new NameValuePair("app_rec", "0"));
				HttpResponse resp = client.doPost(url, referer, params.toArray(new NameValuePair[]{}));
				referer = resp.getURL().toString();
				if (referer.equals("http://www.kaixin001.com/!spiderman/index.php")) {
					user.addStep(RegStep.ADD_XW);
					isChanged = true;
					html = resp.getContent();
					p = Pattern.compile("window[.]location = '(.+?)';");
					m = p.matcher(html);
					if (m.find()) {
						url = KXUtil.HOST_URL + m.group(1);
						client.doGet(url, referer);
						referer = url;
						url = KXUtil.HOST_URL + "/!spiderman/profile_submit.php";
						client.doPost(url, referer, new NameValuePair[]{new NameValuePair("rolekey", "RedSpider")});
						System.out.println("Add XW " + user.getId());
					}
				}
			}
		} finally {
			if (isChanged) {
				KXUtil.saveUsers(users);
			}
		}
	}
}
